const henglang = {
 	host:'https://jewel.gdhenglang.com/api/',
	app_key:'kpXaxw3HAcShy3dv2d43J5SCjcmVss211ZT4CVR543Df',
	showToast:function(title,duration){
		uni.showToast({
			icon: 'none',
			title: title,
			mask: true,
			duration:duration?duration:3000
			//success:success(e) 
		})
	},
	get:function(url,param,check_token,success,fail){
		let request = {
			url:henglang.host+url,
			method:'GET',
			header:{'app-key': henglang.app_key},
			data:param,
			success:(res) => {
				if(res.data.statusCode == '404'){
					henglang.showToast(res.data.data);
					return false;
				}
				if(typeof success == 'function') success(res);
			},
			fail:(res) => {
				if(typeof fail == 'function') fail(res);
			}
		};
		//#ifdef APP-PLUS
		request.header.os = plus.os.name;
		request.header.version = plus.runtime.version;
		//#endif
		let token = uni.getStorageSync('token');
		if(check_token == true && !token){
			uni.showToast({
				title:'请先登录后再操作',
				icon:'none',
				duration:3000,
				success() {
					uni.navigateTo({
						url: '/pages/user/login'
					});
				}
			})
			return false;
		}
		request.header['access-token'] = token?token:'';
		uni.request(request);
	},
	post:function(url,data,check_token,success,fail, that){
		let request = {
			url:henglang.host+url,
			method:'POST',
			header:{'app-key': henglang.app_key},
			success:(res) => {
				if(res.data.statusCode == '404'){
					henglang.showToast(res.data.data);
					return false;
				}
				if(typeof success == 'function') success(res);
			},
			fail:(res) => {
				if(typeof fail == 'function') fail(res);
			}
		};
		//#ifdef APP-PLUS
		request.header.os = plus.os.name;
		request.header.version = plus.runtime.version;
		//#endif
		if(data) request.data = data;
		let token = uni.getStorageSync('token');
		if(check_token == true && !token){
			if(that) {
				that.bIsShowLoading = false;
				that.showNodata = true;
			}
			uni.showToast({
				title:'请先登录后再操作',
				icon:'none',
				duration:3000,
				success() {
					// #ifdef MP-WEIXIN
						setTimeout(function (){
							uni.showModal({
							    title: '提示',
							    content: '检测当前没有登录，是否跳转到登录页面？',
							    success: function (res) {
							        if (res.confirm) {
							            uni.navigateTo({
							            	url: "/pages/jewel/user/wxauth"
							            })
							            uni.removeStorageSync('wx_session_key');
							        }
							    }
							});
						}, 1500)
					// #endif
					//#ifdef APP-PLUS
					uni.navigateTo({
						url: '/pages/user/login'
					});
					//#endif
				}
			})
			return false;
		}
		request.header['access-token'] = token?token:'';
		uni.request(request);
	},
	checkCode:function(e) {
	    let rCode = /^\d{6}$/g;
	    if (rCode.test(e)) {
	        return true;
	    } else {
	        return false;
	    }
	},
	checkPhone:function(e) {
	    let rPhone = /^1[3-9][0-9]\d{8}$/g;
	    if (rPhone.test(e)) {
	        return true;
	    } else {
	        return false;
	    }
	},
	isGoodsTpye:function(){
		return uni.getStorageSync('isGoodType');
	},
	/**
	 * 微信小程序登录
	 * @param json e 微信登录信息 
	 */
	wx_getuserinfo:function(e, callback){
		let errMsg = e.detail.errMsg;
		let wx_session_key = uni.getStorageInfoSync('wx_session_key');
		if(errMsg == "getUserInfo:ok") {
			let userInfo = e.detail.userInfo;
			let signature = e.detail.signature;
			let rawData = e.detail.rawData;
			uni.login({
				success(login_res) {
					if(login_res.errMsg == 'login:ok') {
						let code = login_res.code;
						if(code) {
							let senddata = {signature: signature, rawData: rawData, code: code};
							henglang.get('auth/wxlogin', senddata, false, function(res){
								let resdata = res.data;
								if(resdata.code == '-1') {
									henglang.showToast(resdata.msg, 1000);
								}else{
									uni.setStorageSync('token', resdata.data.token);
									uni.setStorageSync('select_userinfo', 1);
									uni.setStorageSync('wx_session_key', resdata.data.wx_session_key);	// 微信登录token
									typeof callback == 'function' && callback();
								}
							})
						}else{
							henglang.showToast('网络开小差，登录失败', 1000);
						}
					}else{
						henglang.showToast('网络开小差，登录失败', 1000);
					}
				}
			})
		}else{
			henglang.getUserInfo();
		}
	},
	// 获取用户信息
	getUserInfo:function(that){
		henglang.get('member/getInfo', {}, false, function(res){
			let resdata = res.data;
			if(resdata.code == '0') {
				that.userData = resdata.data;
				that.isLogin = true;
				uni.removeStorageSync('select_userinfo');
			}else{
				// // 显示弹框登录
				// // #ifdef MP-WEIXIN
				// 	uni.navigateTo({
				// 		url: "/pages/jewel/user/wxauth"
				// 	})
				// uni.removeStorageSync('wx_session_key');
				// // #endif
			}
		})
	}
	
}
export default henglang